<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc pm f xs">
  
  <xsl:import href="../../../style/base.xsl"/>

  <xsl:template match="pm:usecase_overview">
    <table border="1" cellspacing="1" cellpadding="2" class="overview">
      <xsl:apply-templates select="//doc:chapter[pm:usecase]" mode="overview"/>
    </table>
  </xsl:template>
  
  <xsl:template match="doc:chapter[pm:usecase]" mode="overview">
    <tr>
      <td colspan="3">&#160;&#160;<xsl:value-of select="doc:title" /></td>
    </tr>
    <xsl:apply-templates select="pm:usecase" mode="overview"/>
  </xsl:template>
  
	<xsl:template match="pm:usecase" mode="overview">
		<tr id="{@id}~1">
			<td>
				<a href="#{@id}"><xsl:value-of select="pm:title" /></a>
			</td>
			<td>
				<xsl:apply-templates select="pm:summary" />
			</td>
			<td>
				<xsl:apply-templates select="pm:sequence/pm:step" mode="actors" />
				<xsl:if test="not(pm:sequence/pm:step)">&#160;</xsl:if>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="pm:step" mode="actors">
    <xsl:if test="not(preceding-sibling::pm:step[@actor = current()/@actor])">
      <xsl:if test="position() > 1">, </xsl:if>
      <a href="{@actor}"><xsl:value-of select="//pm:actor[@id = substring(current()/@actor, 2)]/@name" /></a>
    </xsl:if>
  </xsl:template>

  <xsl:template match="pm:usecase">
    <div id="{@id}">
      <h2>
        <xsl:number level="multiple" count="doc:chapter|pm:usecase" format="1.1" />
        <xsl:text> </xsl:text>
        <xsl:value-of select="pm:title" />
      </h2>
      <p><xsl:apply-templates select="pm:summary" /></p>
      <xsl:apply-templates select="pm:precondition" />
      <xsl:if test="pm:sequence/pm:step">
        <table border="0" cellspacing="1" cellpadding="2" class="sequence">
          <xsl:apply-templates select="pm:sequence/pm:step" />
        </table>
      </xsl:if>
      <xsl:apply-templates select="pm:postcondition" />
      <xsl:if test="pm:uc-test">
        <p>Manual Tests</p>
        <table cellspacing="0" class="test_list">
          <thead>
            <tr>
              <th>Time</th>
              <th>Tester</th>
              <th>Result</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="pm:uc-test"/>
          </tbody>
        </table>
      </xsl:if>
    </div>
  </xsl:template>

	<xsl:template match="pm:summary">
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	
	<xsl:template match="pm:actor">
    <div id="{@id}">
      <h2>
        <xsl:number level="multiple" count="doc:chapter | pm:actor" format="1.1" />
        <xsl:text> </xsl:text>
        <xsl:value-of select="@name" />
      </h2>
      <p><xsl:value-of select="." /></p>
      <!-- xsl:variable name="usecases" select="//usecase[step/@actor = current()/@id]"/>
      <xsl:choose>
        <xsl:when test="count($usecases) = count(//usecase)">
          <p>Ist an allen Use Cases beteiligt.</p>
        </xsl:when>
        <xsl:otherwise>
          <p>Ist an den Use Cases <xsl:apply-templates select="$usecases" mode="array"/> beteiligt.</p>
        </xsl:otherwise>
      </xsl:choose-->
    </div>
  </xsl:template>

  <xsl:template match="pm:usecase" mode="array">
    <xsl:choose>
      <xsl:when test="(position() = last()) and (position() > 1)"> und </xsl:when>
      <xsl:when test="position() > 1">, </xsl:when>
    </xsl:choose>
    <a href="#{@id}"><xsl:value-of select="pm:title" /></a>
  </xsl:template>

  <xsl:template match="pm:step">
    <tr id="{@id}">
      <td align="right"><xsl:value-of select="count(preceding-sibling::pm:step)+1" />.</td>
      <td>&#160;</td>
      <td><xsl:value-of select="//pm:actor[@id = substring(current()/@actor, 2)]/@name" /></td>
      <td>&#160;</td>
      <td><xsl:value-of select="pm:description" /></td>
    </tr>
  </xsl:template>

  <xsl:template match="pm:precondition">
    <p>Precondition: <xsl:apply-templates select="pm:val | pm:operand"/></p>
  </xsl:template>

  <xsl:template match="pm:postcondition">
    <p>Postcondition: <xsl:apply-templates select="pm:val | pm:operand"/></p>
  </xsl:template>

  <xsl:template match="pm:val">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="@ref = '#'">
        <span id="{@id}">
          <xsl:value-of select="."/>
          <xsl:if test="normalize-space(.) = ''"><i>--condition--</i></xsl:if>
        </span>
      </xsl:when>
      <xsl:when test="@ref">
        <xsl:variable name="ucUri" select="f:cosmos-uri(@ref, .)"/>
        <xsl:variable name="uc" select="//*[@id = $ucUri/id]" />
        <a id="{@id}" href="{@ref}">
          <xsl:value-of select="$uc/pm:title"/>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <span id="{@id}"><xsl:value-of select="." /></span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="operand">
  </xsl:template>
  
  <xsl:template match="pm:operand" mode="operand">
    <xsl:text> </xsl:text>
    <span class="math"><xsl:value-of select="@type" /></span>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="pm:operand">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
    <span id="{@id}">
    <xsl:if test="count(../*) > 1">(</xsl:if>
    <xsl:apply-templates select="pm:val | pm:operand"/>
    <xsl:if test="count(../*) > 1">)</xsl:if>
      </span>
  </xsl:template>

  <xsl:template match="pm:uc-test">
    <tr id="{@id}">
      <td><xsl:value-of select="format-dateTime(xs:dateTime(@time), '[D01].[M01].[Y] [H01]:[m01]')"/></td>
      <td align="center"><xsl:value-of select="@actor"/></td>
      <td><xsl:value-of select="if (xs:boolean(@passed)) then 'passed' else 'failed'"/></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
